package com.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class BaseController<T> {
	
	public T getModule(String... str){
		
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		Map<String, String[]> map = request.getParameterMap();
		Class<?> clazz = getClass();
		//获取带有泛型的父类
		//Type 是所有java编程语言中所有类型的公共高级接口，包括原始类型、参数化类型、数组类型、类型变量和基本类型。		
		Type type = clazz.getGenericSuperclass();
		ParameterizedType p = (ParameterizedType)type;
		//getActualTypeArguments获取参数化类型的数组，泛型可能有多个
		Type[] param = p.getActualTypeArguments();
		Class<T> c = (Class<T>) param[0];
		try {
			T module = c.newInstance();
			BeanWrapper beanWrapper = new BeanWrapperImpl(module);
			for (Map.Entry<String, String[]> m : map.entrySet()) {  
	            System.out.println("key:"+m.getKey()+" value"+m.getValue()[0]);  
	            beanWrapper.setPropertyValue(m.getKey(), m.getValue());
	        } 
			return module;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}
}
